package com.unknow.first.dapp.admin.job;

import com.unknow.first.dapp.admin.service.AddressContractLogsService;
import lombok.extern.slf4j.Slf4j;
import org.cloud.core.redis.RedisUtil;
import org.cloud.scheduler.constants.MisfireEnum;
import org.cloud.scheduler.job.BaseQuartzJobBean;
import org.cloud.utils.SpringContextUtil;
import org.jetbrains.annotations.NotNull;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.util.Assert;

/**
 * 扫描日志
 */
@Slf4j
public class TUserAddressJob extends BaseQuartzJobBean {

    final private String redisLockerName = "TUserAddressJob.locker";
    RedisUtil redisUtil;
    AddressContractLogsService contractLogsService;

    public TUserAddressJob(AddressContractLogsService contractLogsService) {
        this.contractLogsService = contractLogsService;
    }

    @Override
    protected void init() {
        this.jobName = "从日志中获取数据保存到推荐图谱中";
        jobData.put("description", "从日志中获取数据保存到推荐图谱中");
        this.jobTime = "0/30 * * * * ?";
        setMisfire(MisfireEnum.CronScheduleMisfireEnum.MISFIRE_INSTRUCTION_DO_NOTHING);
    }

    @Override
    protected void executeInternal(@NotNull JobExecutionContext jobExecutionContext) throws JobExecutionException {

        if (redisUtil == null) {
            redisUtil = SpringContextUtil.getBean(RedisUtil.class);
        }

        Assert.isTrue(redisUtil != null, "redisutil 未获取到");

        if (!redisUtil.getLock(redisLockerName, 60 * 60 * 1000)) {
            return;
        }

        try {
            contractLogsService.scanLogsToAddressTable();
        } finally {
            redisUtil.releaseLock(redisLockerName);
        }


    }
}